﻿using AutoMapper;
using B.S.Warehouse.Domain;
using B.S.Warehouse.ErrorCord;
using B.S.Warehouse.Read.Api.Application.Command.OutboundCommand;
using MediatR;
using SqlSugar;

namespace B.S.Warehouse.Read.Api.Application.Handelr.OutboundHandler
{
    public class GetOutboundHandler : IRequestHandler<GetOutboundCommand, ApiResult<ApiPaging<Outbounds>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public GetOutboundHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<ApiResult<ApiPaging<Outbounds>>> Handle(GetOutboundCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<Outbounds>> result = new ApiResult<ApiPaging<Outbounds>>();
            var query = await db.Queryable<Outbounds>().Where(x => !x.IsDel).ToListAsync();
            // 联查操作
            // var query = db.Queryable<Outbound, OutboundDetails>((o, od) => new JoinQueryInfos(
            //     JoinType.Left, o.Id == od.OutboundDetailsId
            // ))
            //.Select((o, od) => new OutboundWithDetailsDto
            //{
            //    // 映射出库表字段
            //    Id = o.Id,
            //    DocumentSubject = o.DocumentSubject,
            //    DocumentNumber = o.DocumentNumber,
            //    OutboundWarehouseKeeper = o.OutboundWarehouseKeeper,
            //    SourceDocument = o.SourceDocument,
            //    AssociatedUnitName = o.AssociatedUnitName,
            //    Applicant = o.Applicant,
            //    ApplicationTime = o.ApplicationTime,
            //    OutboundCategory = o.OutboundCategory,
            //    // 映射出库明细表字段
            //    ApplicationOutboundQuantity = od.ApplicationOutboundQuantity,
            //})
            //.Where((o => !o.IsDel));
            if (!string.IsNullOrEmpty(request.DocumentSubject))
            {
                query = query.Where(x => x.DocumentSubject.Contains(request.DocumentSubject)).ToList();
            }
            // 根据条件过滤
            if (!string.IsNullOrEmpty(request.DocumentNumber))
            {
                query = query.Where(x => x.DocumentNumber.Contains(request.DocumentNumber)).ToList();
            }
            if (!string.IsNullOrEmpty(request.OutboundCategory))
            {
                query = query.Where(x => x.OutboundCategory.Contains(request.OutboundCategory)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Applicant))
            {
                query = query.Where(x => x.Applicant.Contains(request.Applicant)).ToList();
            }
            if (!string.IsNullOrEmpty(request.OutboundWarehouseKeeper))
            {
                query = query.Where(x => x.OutboundWarehouseKeeper.Contains(request.OutboundWarehouseKeeper)).ToList();
            }

            if (request.ApplicationTime != null)
            {
                query = query.Where(x => x.ApplicationTime.Date == request.ApplicationTime.Value.Date).ToList();
            }
            int totalCount = query.Count();
            int PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            query = query.OrderByDescending(a => a.OutboundId).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            ApiPaging<Outbounds> paging = new ApiPaging<Outbounds>()
            {
                totalCount = totalCount,
                PageCount = PageCount,
                Result = query.ToList(),

            };

            result.Code = ApiEnum.出库申请显示成功;
            result.MAG = ApiEnum.出库申请显示成功.ToString();
            result.data = paging;
            return result;
        }
    }
}